package com.iwhalecloud.bss.kite.cucc.common.util;

import java.io.ByteArrayInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import com.iwhalecloud.bss.kite.cart.common.util.KiteLogProducerUtil;
import com.iwhalecloud.bss.kite.common.cache.AbstractSecondLevelCache;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteMapUtils;
import com.iwhalecloud.bss.kite.log.client.dto.InfKiteServiceLogReqDTO;
import org.apache.axis.client.Call;
import org.apache.axis.message.SOAPEnvelope;

import com.iwhalecloud.bss.kite.client.api.inf.IInfKiteServiceLogService;
import com.iwhalecloud.bss.kite.client.dto.inf.InfKiteServiceLogDTO;
import com.iwhalecloud.bss.kite.common.constant.TableConsts;
import com.iwhalecloud.bss.kite.common.util.KiteDateUtils;
import com.iwhalecloud.bss.kite.common.util.SeqUtils;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;

/**
* @Description: TODO
*
* @author lyx
* @date 2020年11月10日
* @version V1.0
* @Package com.iwhalecloud.bss.kite.cucc.common.util.webService
*/
public class WebServiceUtil {

    /**  字符集 **/
    private static final String ENCODING_STYLE = "UTF-8";

    public static void main(String[] args) {
        Date d = new Date();
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        System.out.println("PROC_ID:" + String.valueOf(d.getTime()) + new Random().nextInt(99999));
        System.out.println("TRANS_IDO:" + String.valueOf(d.getTime()) + new Random().nextInt(99999));
        System.out.println("${PROCESS_TIME}:" + sdf1.format(d));
        System.out.println("${CUTOFFDAY}:" + sdf2.format(d));
    }

    public static SOAPEnvelope invoke(String reqString, String url) throws Exception {
        Call call = new Call(new org.apache.axis.client.Service());
        SOAPEnvelope req = new SOAPEnvelope(new ByteArrayInputStream(reqString.trim().getBytes(ENCODING_STYLE)));
        call.setTargetEndpointAddress(url);
        SOAPEnvelope soap = call.invoke(req);
        InfKiteServiceLogDTO logDto = initLog(url, req.getAsString());
        logDto.setOutXml(soap.getAsString());
        addInfServiceLog(logDto);
        return soap;

    }

    private static InfKiteServiceLogDTO initLog(String svcCode, String reqParam) {
        InfKiteServiceLogDTO logDto = new InfKiteServiceLogDTO();
        logDto.setLogId(SeqUtils.getNext(TableConsts.InfKiteServiceLog));
        logDto.setSvcCode(svcCode);
        logDto.setAppKey("KITE");
        logDto.setReqTime(KiteDateUtils.dateToLocalDateTime(new Date()));
        logDto.setInXml(reqParam);
        logDto.setTransactionId(ContextUtil.getHttpSessoinId());
        return logDto;
    }

    private static void addInfServiceLog(InfKiteServiceLogDTO logDto) {
        logDto.setRspTime(KiteDateUtils.dateToLocalDateTime(new Date()));
        //解决冲突
        AbstractSecondLevelCache dcSystemParamCache = SpringUtil.getBean("dcSystemParamCache", AbstractSecondLevelCache.class);
        if (dcSystemParamCache != null && "0".equals(KiteMapUtils.beanToMap(dcSystemParamCache.defaultGetObject("INF_KITE_SERVICE_LOG_BY_MQ")).get("paramVal"))) {
            IInfKiteServiceLogService service = SpringUtil.getBean(IInfKiteServiceLogService.class);
            service.addInfKiteServiceLog(logDto);
        }
        else {
            InfKiteServiceLogReqDTO serviceLogReqDTO = KiteBeanUtils.transform(logDto, InfKiteServiceLogReqDTO.class);
            KiteLogProducerUtil.addInfKiteServiceLogSync(serviceLogReqDTO);
        }
    }
}
